package com.two.screen.water.balance.ui.activity

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.jakewharton.rxbinding2.view.RxView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.bean.database.UserDB
import com.two.screen.water.balance.bean.database.UserDBDao
import com.two.screen.water.balance.image.ImageManger
import com.two.screen.water.balance.listener.MyObserver
import com.two.screen.water.balance.net.api.DataApi
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.ui.activity.base.BaseActivity
import com.two.screen.water.balance.utils.ARouterUtils
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.DialogUtil
import com.two.screen.water.balance.utils.RxUtils
import com.two.screen.water.balance.utils.eventbus.Event
import com.two.screen.water.balance.utils.eventbus.EventBusUtil
import com.two.screen.water.balance.utils.eventbus.EventCode
import com.two.screen.water.balance.widget.dialog.OrderUploadDialog
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import kotlinx.android.synthetic.main.activity_function_menu.*
import java.util.concurrent.TimeUnit


@Route(path = ARouterUtils.FunctionMenuActivity)
class FunctionMenuActivity : BaseActivity() {
    override fun getContentLayout(): Any = R.layout.activity_function_menu

    override fun initView(savedInstanceState: Bundle?) {
    }

    override fun initData() {
        getUserData()
    }

    private fun getUserData() {
        d("getUserId:${DataApi.mUserId}")
        Observable.create(ObservableOnSubscribe<UserDB> { e ->
            val userDB = App.mDaoSession?.userDBDao?.queryBuilder()
                ?.where(UserDBDao.Properties.Id.eq(DataApi.mUserId))
                ?.offset(0)?.limit(1)
                ?.list()
                ?.firstOrNull()
            if (userDB != null) e.onNext(userDB) else e.onError(Throwable("找不到用户!"))
        })
            .compose(RxUtils.io())
            .compose(RxUtils.aLifecycle(this@FunctionMenuActivity))
            .subscribe(object : MyObserver<UserDB>() {
                @SuppressLint("SetTextI18n", "CheckResult")
                override fun onNext(userDB: UserDB) {
                    ImageManger.setCircleImg(this@FunctionMenuActivity, userDB.logo, mUserLogoIv)
                    mUserNameTv.text = userDB.userName.plus("(${userDB.accounts})")
                    mLoginTimeTv.text = "登录时间：${userDB.updateTimeName}"
                    RxView.clicks(mShiftingOfDutyTv)
                        .throttleFirst(1, TimeUnit.SECONDS)
                        .subscribe {
                            //跳转页面
//                            ARouter.getInstance()
//                                .build(ARouterUtils.JiaoJieActivity)
//                                .navigation()

                            AlertDialog.Builder(this@FunctionMenuActivity)
                                .setMessage("是否退出登录？")
                                .setCancelable(false)
                                .setPositiveButton("确认") { dialog, _ ->
                                    CommUtils.logout(this@FunctionMenuActivity)
                                    dialog.dismiss()
                                }
                                .setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                                .show().apply {
                                    DialogUtil.hideNavigationBar(this.window)
                                }

                        }
                }
            })

    }

    @SuppressLint("ClickableViewAccessibility", "CheckResult")
    override fun initListener() {
        mGoBackLayout.setOnClickListener { goBack() }
        mServerOrderLayout.setOnClickListener {
            ARouter.getInstance()
                .build(ARouterUtils.OrderServerActivity)
                .navigation()
        }
        mLocalOrderLayout.setOnClickListener {
            ARouter.getInstance()
                .build(ARouterUtils.OrderLocalActivity)
                .navigation()
        }
        mSystemSettingLayout.setOnClickListener {
            ARouter.getInstance()
                .build(ARouterUtils.SystemSettingActivity)
                .navigation()
        }
        mLocalOrderUploadLayout.setOnClickListener {
            OrderUploadDialog(this@FunctionMenuActivity).show()
        }

        mDateSyncLayout.setOnClickListener {
            //TODO：重启app
            ARouter.getInstance()
                .build(ARouterUtils.LoginActivity)
                .withBoolean("mIsAutoLogin", true)
                .navigation()
            EventBusUtil.sendEvent(Event(EventCode.FINISH_INDEX))
            finish()
        }
    }

}